package com.example.demo.controller;


import com.example.demo.entity.ProductDTO;
import com.example.demo.services.ProduceWpInRepairService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/produceRepair")
public class ProduceWpInRepairController {
    public final ProduceWpInRepairService service;

    public ProduceWpInRepairController(ProduceWpInRepairService service) {
        this.service = service;

    }

    @PostMapping("/findByNo")
    public ResponseEntity<ProductDTO> findByNo(@RequestBody Map<String, String> payload) {
        String billNo = payload.get("billNo");
        ProductDTO res = service.findByNo(billNo);
        if (res != null) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }

    @PostMapping("/audit")
    public int updateStatus(@RequestBody Map<String, String> payload) {
        String billNo = payload.get("billNo");
        String userID = payload.get("userID");
        String userName = payload.get("userName");
        String modeID = payload.get("modeID");
        //System.out.println(billNo + userID + userName + modeID);
        return service.updateStatus(billNo, userID, userName, modeID);
    }

    @PostMapping("/waOptions")
    public List<Map<String, String>> getWarehouseOptions(@RequestBody Map<String, String> payload) {
        String property = payload.get("property");
        //System.out.println(property);
        return service.getAllWorkArea(property).stream()
                .map(w -> {
                    Map<String, String> option = new HashMap<>();
                    option.put("label", w.getWorkingAreaName());
                    option.put("value", w.getWorkingAreaCode());
                    return option;
                })
                .collect(Collectors.toList());
    }

    @PostMapping("/userOptions")
    public List<Map<String, String>> getUserWorkAreaOptions(@RequestBody Map<String, String> payload) {
        String property = payload.get("property");
        String userID = payload.get("userID");
        //System.out.println(property);
        return service.getUserWorkArea(property, userID).stream()
                .map(w -> {
                    Map<String, String> option = new HashMap<>();
                    option.put("label", w.getWorkingAreaName());
                    option.put("value", w.getWorkingAreaCode());
                    return option;
                })
                .collect(Collectors.toList());
    }
}
